<?php
/*
 +----------------------------------------------------------------------
 + Title        : BaseModel 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/2/27 17:39
 + Last-time    : 2021/2/27 17:39+ chen2
 + Desc         : BaseModel
 +----------------------------------------------------------------------
*/


namespace app\common\model;


use randy\tools\units\MyQuery;
use think\Model;

/**
 * @mixin MyQuery
 */
class BaseModel extends Model
{
    protected $dateFormat         = 'Y-m-d H:i:s';
    protected $autoWriteTimestamp = 'datetime';

    public static function build()
    {
        return new static();
    }

    public static function onBeforeInsert($model)
    {
        $model->created_id = get_client_id();
        $model->created_name = get_client_name();
        $model->updated_id = get_client_id();
        $model->updated_name = get_client_name();
        return true;
    }

    public static function onBeforeUpdate($model)
    {
        $model->updated_id = get_client_id();
        $model->updated_name = get_client_name();
        return true;
    }


    protected function readTransform($value, $type)
    {
        if (is_null($value)) {
            return;
        }

        if (is_array($type)) {
            [$type, $param] = $type;
        } elseif (strpos($type, ':')) {
            [$type, $param] = explode(':', $type, 2);
        }

        switch ($type) {
            case 'integer':
                $value = (int) $value;
                break;
            case 'string':
                $value = (string) $value;
                break;
            case 'float':
                if (empty($param)) {
                    $value = (float) $value;
                } else {
                    $value = (float) number_format($value, (int) $param, '.', '');
                }
                break;
            case 'boolean':
                $value = (bool) $value;
                break;
            case 'timestamp':
                if (!is_null($value)) {
                    $format = !empty($param) ? $param : $this->dateFormat;
                    $value  = $this->formatDateTime($format, $value, true);
                }
                break;
            case 'datetime':
                if (!is_null($value)) {
                    $format = !empty($param) ? $param : $this->dateFormat;
                    $value  = $this->formatDateTime($format, $value);
                }
                break;
            case 'json':
                $value = json_decode($value, true);
                break;
            case 'array':
                $value = empty($value) ? [] : json_decode($value, true);
                break;
            case 'object':
                $value = empty($value) ? new \stdClass() : json_decode($value);
                break;
            case 'serialize':
                try {
                    $value = unserialize($value);
                } catch (\Exception $e) {
                    $value = null;
                }
                break;
            default:
                if (false !== strpos($type, '\\')) {
                    // 对象类型
                    $value = new $type($value);
                }
        }

        return $value;
    }



}